<?php

class LinkController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
		$frmLink=new Form_LinkSubmitForm();
		$frmLink->setAction($this->getRequest()->getBaseUrl().'/link/');
		$frmLink->setMethod('post');
		if($this->getRequest()->isPost() && $frmLink->isValid($_POST)){
			$linkModel=new Model_Link();
			$str=time();
			$v_code=substr($str,strlen($str)-8,strlen($str));
			$result=$linkModel->createLink($_POST['title'],
				$_POST['url'],$_POST['description'],$_POST['category'],$_POST['name'],$_POST['email'],$v_code);
			$categoryModel=new Model_Category();
			$result_cat=$categoryModel->addNewLinkToCategory($_POST['category']);
			if($result && $result_cat){
				//send confirmation email
				/*
				$mail=new Zend_Mail();
				$message="We have recieved a link submitted providing your email address.
				 If this is a mistake, just ignore this message. Otherwise confirm your
				 submission by visiting following url <a href='".$this->_request->getBaseUrl()."/link/verify/code/".$v_code."'>Verify your
				 Submission </a>";
				 $fromName="SLDigits Linkdir";
				 $to=$_POST['email'];
				 $subject="Link submission verification";
				 $mail->setBodyText($message);
				 $mail->addTo($to);
				 $mail->setSubject($subject);
				 $mail->setFrom($fromName);
				 try{
					$mail->send();
					//forward to thnx page						
				 }catch(Zend_Mail_Exception $e){
					//ann erro occured
				 }	
					*/
					$this->_forward('thnx');
			}			
		}
		$this->view->form=$frmLink;
    }
	public function thnxAction(){
		
	}
	public function listAction(){
		$category=$this->_request->getParam('category',1);
		$status=$this->_request->getParam('status',2);
		$filter['category']=$category;
		$filter['status']=$status;
		$linkModel=new Model_Link();
		$adapter=$linkModel->fetchLinksPaginatorAdapter($filter);
		$paginator=new Zend_Paginator($adapter);
		$paginator->setItemCountPerPage(5);
		$page=$this->_request->getParam('page',1);
		$paginator->setCurrentPageNumber($page);
		$this->view->paginator=$paginator;		
	}
	public function verifyAction(){
		$code=$this->_request->getParam('code',1);
		$linkModel=new Model_Link();
		$result=$linkModel->verifyLink($code);
		if($result){
		
		}else{
		
		}
	}
}
?>